package sy.controller;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sy.jackson.SysBuildingMixin;
import sy.jackson.SysProjectMixin;
import sy.model.AppAccountVerificatCode;
import sy.model.SysAccount;
import sy.model.SysBuilding;
import sy.model.SysProject;
import sy.model.SysUser;
import sy.service.AppAccountVerificatCodeServiceI;
import sy.service.AppBaseServiceI;
import sy.service.SysAccountServiceI;
import sy.service.SysUserServiceI;
import sy.util.Encrypt;
import sy.util.SendMessage;
import sy.util.ResultJson;
@RestController
@RequestMapping("v1/appBase")
public class AppBaseController {
	@Autowired
	private AppBaseServiceI appBaseService;
	
	private static final ObjectMapper objectMapper = new ObjectMapper();
	
	/**
	 * @Title: getVerificatCode
	 * @Description: TODO
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月14日 下午4:42:52
	 * @throws
	 */
	@RequestMapping(value="/register/getVerificatCode", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson getVerificatCode(@RequestParam("telephone") String telephone){
		/**
		 * 1.查找该手机是否已注册过；
		 * 2.没有注册，则先生成一个6位数的验证码,写入AppAccountVerificatCode表，再调用短信发送接口发送到传过来的手机
		 */
		ResultJson resultJson = appBaseService.getVerificatCode(telephone);
		return resultJson;
	}
	/**
	 * 
	 * @Title: testVerificatCode
	 * @Description: TODO
	 * @param: @param map
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月15日 上午9:17:48
	 * @throws
	 */
	@RequestMapping(value="/register/testVerificatCode", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson testVerificatCode(@RequestParam("telephone") String telephone, @RequestParam("verificatCode") String verificatCode){
		ResultJson resultJson = appBaseService.testVerificatCode(telephone, verificatCode);
		return resultJson;
	}
	/**
	 * 
	 * @Title: register
	 * @Description: TODO
	 * @param: @param map
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月15日 上午10:27:34
	 * @throws
	 */
	@RequestMapping(value="/register", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson register(@RequestParam("telephone") String telephone, @RequestParam("password") String password, @RequestParam("verificatCode") String verificatCode){
		ResultJson resultJsonTestCode = appBaseService.testVerificatCode(telephone, verificatCode);
		ResultJson resultJson = new ResultJson();
		/**
		 * 验证通过，执行注册
		 */
		if (resultJsonTestCode.isState()){
			resultJson = appBaseService.register(telephone, password);
		} else {
			resultJson.setState(false);
			resultJson.setErrorCode(-6);
			resultJson.setMessage("您没有通过验证码验证，无法注册！");
		}
		return resultJson;
	}
	/**
	 * 
	 * @Title: login
	 * @Description: TODO
	 * @param: @param map
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月15日 上午10:37:11
	 * @throws
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String login(@RequestParam("telephone") String telephone, @RequestParam("password") String password){
		ResultJson resultJson = appBaseService.login(telephone, password);
		objectMapper.addMixInAnnotations(SysProject.class, SysProjectMixin.BasicInfo.class);
		try {
			return objectMapper.writeValueAsString(resultJson);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";	
	}
	/**
	 * 
	 * @Title: appLogout
	 * @Description: TODO
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月31日 上午9:06:05
	 * @throws
	 */
	@RequestMapping(value="/appLogout", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson appLogout(@RequestParam("token") String token){
		ResultJson resultJson = appBaseService.appLogout(token);
		return resultJson;
	}
	/**
	 * 
	 * @Title: modifyPassword
	 * @Description: TODO
	 * @param: @param telephone
	 * @param: @param password
	 * @param: @param token
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年8月31日 上午9:35:57
	 * @throws
	 */
	@RequestMapping(value="/modifyPassword", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson modifyPassword( @RequestParam("telephone")String telephone,  @RequestParam("password")String password){
		ResultJson resultJson = appBaseService.modifyPassword(telephone, password);
		return resultJson;
	}
	/**
	 * 
	 * @Title: jgPush
	 * @Description: TODO
	 * @param: @param alias
	 * @param: @param msg
	 * @param: @return     
	 * @return: ResultJson     
	 * @author:  xieyuanqiu
	 * @date: 2017年10月2日 下午3:33:27
	 * @throws
	 */
	@RequestMapping(value="/jgPush", method=RequestMethod.POST)
	@ResponseBody
	public ResultJson jgPush( @RequestParam("alias")String alias,  @RequestParam("msg")String msg,  @RequestParam(value="pushToWebMsg", required=false)String pushToWebMsg){
		ResultJson resultJson = appBaseService.jgPush(alias, msg, pushToWebMsg);
		return resultJson;
	}
}
